/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Forms;

import BusinessEntity.BE_CasoPruebaEstimacion;
import BusinessEntity.BE_Personal;
import BusinessEntity.BE_Sistema;
import BusinessEntity.BE_Usuario;
import BusinessLogic.BL_Sistema;
import Comun.Funciones_Comunes;
import java.awt.event.ItemEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.ListSelectionModel;

/**
 *
 * @author richard.saldana
 */
public class Frm_CasoPrueba extends javax.swing.JInternalFrame {

    private JInternalFrame frmPadre;
    //Personal
    private BE_Personal objBEPersonal;
    //Usuario
    private BE_Usuario objBEUsuario;
    //combo Sistema
    private ArrayList<BE_Sistema> aobjBE_comboSistema;
    //combo Subsistema
    private ArrayList<BE_Sistema> aobjBE_comboSubsistema;
    //private listaCasosPrueba
    private ArrayList<BE_CasoPruebaEstimacion>  aobjBE_CasosPruebaEstimacion;
    /**
     * Creates new form Frm_CasoPrueba
     */
    public Frm_CasoPrueba(JInternalFrame frmVentana) throws InstantiationException, IllegalAccessException, SQLException {
        initComponents();
        setDefaultCloseOperation(0);
        this.frmPadre = frmVentana;
        this.objBEPersonal = ((Frm_Estimacion) this.frmPadre).getObjBEPersonal();
        this.objBEUsuario = ((Frm_Estimacion) this.frmPadre).getObjBEUsuario();
        this.aobjBE_CasosPruebaEstimacion = ((Frm_Estimacion) this.frmPadre).getAobjBE_CasosPruebaEstimacion();
        inicializar();
    }
    
         private void inicializar() throws InstantiationException, IllegalAccessException, SQLException{

        //combo Sistema
        setAobjBE_comboSistema(BL_Sistema.obtenerInstancia().obtenerSistemas());
        if(this.aobjBE_comboSistema != null){
            cmbSistema.removeAllItems();
            this.cmbSistema.addItem("<Seleccionar>");
            for (int i = 0; i < aobjBE_comboSistema.size(); i++) {
                this.cmbSistema.addItem(aobjBE_comboSistema.get(i));
                
            }
        }
    }
         
         private void llenarComboSubsistema(int intCodigoSistema) throws InstantiationException, IllegalAccessException, SQLException{
        //combo Sistema
        setAobjBE_comboSubsistema(BL_Sistema.obtenerInstancia().obtenerSubsistemas(intCodigoSistema));
        if(this.aobjBE_comboSubsistema != null){
            cmbSubsistema.removeAllItems();
            this.cmbSubsistema.addItem("<Seleccionar>");
            for (int i = 0; i < aobjBE_comboSubsistema.size(); i++) {
                this.cmbSubsistema.addItem(aobjBE_comboSubsistema.get(i));
                
            }
        }
         }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        lblDescripcion = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtxDescripcion = new javax.swing.JTextArea();
        cmbSistema = new javax.swing.JComboBox();
        lblSistema = new javax.swing.JLabel();
        lblSubsistema = new javax.swing.JLabel();
        cmbSubsistema = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalle Caso Prueba"));

        lblNombre.setText("Nombre:");

        lblDescripcion.setText("Descripción:");

        jtxDescripcion.setColumns(20);
        jtxDescripcion.setRows(5);
        jScrollPane3.setViewportView(jtxDescripcion);

        cmbSistema.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbSistemaItemStateChanged(evt);
            }
        });
        cmbSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSistemaActionPerformed(evt);
            }
        });

        lblSistema.setText("Sistema:");

        lblSubsistema.setText("Subsistema:");

        cmbSubsistema.setEnabled(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcion)
                    .addComponent(lblNombre)
                    .addComponent(lblSistema)
                    .addComponent(lblSubsistema))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(cmbSistema, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane3)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbSubsistema, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(lblDescripcion)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSistema)
                    .addComponent(cmbSistema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSubsistema)
                    .addComponent(cmbSubsistema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11))
        );

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/guardar32.png"))); // NOI18N
        btnAceptar.setToolTipText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/eliminar32.png"))); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(291, Short.MAX_VALUE)
                .addComponent(btnAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        boolean bolTamahoNombre = (Funciones_Comunes.validarTextField(txtNombre.getText().trim(), "mayor", 0, lblNombre.getText()));
        boolean bolTamahoDescripcion = (Funciones_Comunes.validarTextField(jtxDescripcion.getText().trim(), "mayor", 0, lblDescripcion.getText()));
        boolean bolComboSistema = Funciones_Comunes.validarCombo(cmbSistema.getSelectedItem().toString(), "<Seleccionar>", lblSistema.getText());
        boolean bolComboSubsistema = Funciones_Comunes.validarCombo(cmbSubsistema.getSelectedItem().toString(), "<Seleccionar>", lblSubsistema.getText());
        
        if(bolTamahoNombre && bolTamahoDescripcion && bolComboSistema && bolComboSubsistema){
            BE_CasoPruebaEstimacion objBE_CasoPruebaEstimacion = new BE_CasoPruebaEstimacion();
            BE_Sistema objBE_Subsistema = (BE_Sistema) this.cmbSubsistema.getSelectedItem();
            BE_Sistema objBE_Sistema = (BE_Sistema) this.cmbSistema.getSelectedItem();
            objBE_Sistema.setObjSubsistema(objBE_Subsistema);
            objBE_CasoPruebaEstimacion.setStrNombre(txtNombre.getText().trim());
            objBE_CasoPruebaEstimacion.setStrDescripcion(jtxDescripcion.getText().trim());
            objBE_CasoPruebaEstimacion.setObjBE_Sistema(objBE_Sistema);
           
            this.aobjBE_CasosPruebaEstimacion.add(objBE_CasoPruebaEstimacion);
            
            ((Frm_Estimacion)this.frmPadre).setAobjBE_CasosPruebaEstimacion(this.aobjBE_CasosPruebaEstimacion);
            ((Frm_Estimacion) (this.frmPadre)).setVisible(true); 
            try {
                ((Frm_Estimacion) (this.frmPadre)).cargarListaCasosPrueba();
            } catch (InstantiationException ex) {
                Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            this.dispose();
            
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        ((Frm_Estimacion) (this.frmPadre)).setVisible(true);
        ((Frm_Estimacion) (this.frmPadre)).tblPruebas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        try {
            ((Frm_BandejaTarea) (this.frmPadre)).cargarListaRequerimientos(this.getObjBEPersonal());
        } catch (InstantiationException ex) {
            Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.dispose();
        // TODO add your handling code here:
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void cmbSistemaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbSistemaItemStateChanged
        if(evt.getStateChange() == ItemEvent.SELECTED){
            boolean bolComboPrioridad = Funciones_Comunes.validarComboInicializar(cmbSistema.getSelectedItem().toString(), "<Seleccionar>");
            
            if(bolComboPrioridad){
                BE_Sistema objBE_Sistema = (BE_Sistema) this.cmbSistema.getSelectedItem();
                cmbSubsistema.setEnabled(true);
                try {
                    llenarComboSubsistema(objBE_Sistema.getIdSistema());
                } catch (InstantiationException ex) {
                    Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }else{
                try {
                    llenarComboSubsistema(0);
                } catch (InstantiationException ex) {
                    Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Frm_CasoPrueba.class.getName()).log(Level.SEVERE, null, ex);
                }
                cmbSubsistema.setEnabled(false);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbSistemaItemStateChanged

    private void cmbSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSistemaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbSistemaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbSistema;
    private javax.swing.JComboBox cmbSubsistema;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jtxDescripcion;
    private javax.swing.JLabel lblDescripcion;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblSistema;
    private javax.swing.JLabel lblSubsistema;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the objBEPersonal
     */
    public BE_Personal getObjBEPersonal() {
        return objBEPersonal;
    }

    /**
     * @param objBEPersonal the objBEPersonal to set
     */
    public void setObjBEPersonal(BE_Personal objBEPersonal) {
        this.objBEPersonal = objBEPersonal;
    }

    /**
     * @return the objBEUsuario
     */
    public BE_Usuario getObjBEUsuario() {
        return objBEUsuario;
    }

    /**
     * @param objBEUsuario the objBEUsuario to set
     */
    public void setObjBEUsuario(BE_Usuario objBEUsuario) {
        this.objBEUsuario = objBEUsuario;
    }

    /**
     * @return the aobjBE_comboSistema
     */
    public ArrayList<BE_Sistema> getAobjBE_comboSistema() {
        return aobjBE_comboSistema;
    }

    /**
     * @param aobjBE_comboSistema the aobjBE_comboSistema to set
     */
    public void setAobjBE_comboSistema(ArrayList<BE_Sistema> aobjBE_comboSistema) {
        this.aobjBE_comboSistema = aobjBE_comboSistema;
    }

    /**
     * @return the aobjBE_comboSubsistema
     */
    public ArrayList<BE_Sistema> getAobjBE_comboSubsistema() {
        return aobjBE_comboSubsistema;
    }

    /**
     * @param aobjBE_comboSubsistema the aobjBE_comboSubsistema to set
     */
    public void setAobjBE_comboSubsistema(ArrayList<BE_Sistema> aobjBE_comboSubsistema) {
        this.aobjBE_comboSubsistema = aobjBE_comboSubsistema;
    }

    /**
     * @return the aobjBE_CasosPruebaEstimacion
     */
    public ArrayList<BE_CasoPruebaEstimacion> getAobjBE_CasosPruebaEstimacion() {
        return aobjBE_CasosPruebaEstimacion;
    }

    /**
     * @param aobjBE_CasosPruebaEstimacion the aobjBE_CasosPruebaEstimacion to set
     */
    public void setAobjBE_CasosPruebaEstimacion(ArrayList<BE_CasoPruebaEstimacion> aobjBE_CasosPruebaEstimacion) {
        this.aobjBE_CasosPruebaEstimacion = aobjBE_CasosPruebaEstimacion;
    }
}
